#!/usr/bin/env python
# -*- coding: cp936 -*-

from ftplib import *;

import StringIO, sys, getopt,os,urllib, traceback,re,cgi
import cgitb
import time

WEBURL = "http://www.newsmth.net/"

def mkdir(dt):
    try:
        os.makedirs(dt)
    except:
        pass

curpath = sys.argv[0]
BOARD = curpath.rsplit(os.sep,2)[-2]
DIRPREFIX = curpath.rsplit(os.sep,1)[0]+os.sep+"".join(["%02d"%x for x in time.localtime()[:3]])+os.sep
mkdir(DIRPREFIX)
DAY = "30"
maxnum = 5

# logger
import logging,pdb
logger = logging.getLogger('smthweb.log')
__hdlr = logging.FileHandler('smthweb.log')
__formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
__hdlr.setFormatter(__formatter)
logger.addHandler(__hdlr)
logger.setLevel(logging.INFO)

__olderror = logger.error
def __newerror(*karg, **kargs):
    __olderror(*karg, **kargs);
    exc = pdb.traceback.format_exc()
    if exc:
        __olderror(exc)
        print exc

logger.error = __newerror
##########################################

    
def err_usage():
    print 'usage: %s [-m num_of_news]'%sys.argv[0]
    sys.exit(1)

def parseURL(url, userid='unknown', htmlfile = None):
    orifile = urllib.urlopen(url)
    for line in orifile.readlines():
        try:
            line=line.strip()
            mat = re.search("^var o = new conWriter\((\d+), '(\w+)', (\d+), (\d+), (\d+), (\d+), '[^']+', (\d+)\);",line)
            if mat:
                logger.debug("conWriter found for bid@id: %s@%s"%(mat.group(4),mat.group(3)))
                ftype = int(mat.group(1))# 0 almost
                board = mat.group(2)     # board name
                bid = int(mat.group(3))  # board id
                iid = int(mat.group(4))  # this id
                gid = int(mat.group(5))  # 
                reid = int(mat.group(6)) # topic No
                num = int(mat.group(7))  # pst No in this board

            # function attWriter(bid, id, ftype, num, cacheable)
            mat = re.search("^att = new attWriter\((\d+),(\d+),(\d+),(\d+),(\d+)\);",line)
            if mat:
                cacheable = int(mat.group(5))
                if bid!=int(mat.group(1)) or iid!=int(mat.group(2)) or ftype!=int(mat.group(3)) or num!=int(mat.group(4)):
                    logger.warning( "some discripency !!! check this!! ")
                    logger.warning("attWriter found for bid@id: %s@%s"%(mat.group(1),mat.group(2)))
                isAttached = True

            # contents
            mat = re.search("prints\((.*);o\.h\(0\);o\.t\(\)",line);
            if mat:
                ctn = mat.group(1)
                ctn = toPrint(ctn)
                if isAttached:
                    attlist = re.findall("attach\((\S+), (\d+), (\d+)\)",ctn)
                    for att in attlist:
                        attname = att[0].replace("'","")
                        outfilename = DIRPREFIX+userid+"_"+str(iid)+"_"+attname
                        if os.path.exists(outfilename) and os.path.getsize(outfilename) == int(att[1]):
                            continue
                        atturl = WEBURL+"att.php?"
                        if not cacheable:
                            atturl += "n"
                        elif int(att[1])>51200:
                            atturl += "p"
                        else:
                            atturl += "s"
                        atturl += "."+str(bid)+"."+str(iid)+"."+att[2]
                        urllib.urlretrieve(atturl,outfilename)
                        print "saved %s  @  %s"%(outfilename,atturl)
                        if htmlfile:
                            localdir = DIRPREFIX.rsplit(os.sep,2)[-2]+os.sep+userid+"_"+str(iid)+"_"+attname
                            htmlfile.write(userid+"_"+attname+'<br><a href="%s"><img src="%s"></a><br>\n'%(localdir,localdir))
                            htmlfile.flush()
        except not KeyboardInterrupt:
            logger.error(None)
            pass

def toPrint(s):
    s = re.sub(r'\\r[\[\d;]+[a-zA-Z]',"",s)
    s = re.sub(r'\\n',os.linesep,s)
    s = re.sub(r'\\\\',r"\\",s)
    s = re.sub(r'\\\'',"'",s)
    s = re.sub(r'\\"','"',s)    
    return s
# 	s = s.replace(/\x20\x20/g, " &nbsp;").replace(/\n /g, "\n&nbsp;");
# 	s = s.replace(/\n(: [^\n]*)/g, "<br/><span class=\"f006\">$1</span>");
# 	if (gKon && s.length > 0) {
# 		s = s.split("\n").join("<br/>");
# 	} else {
# 		s = s.replace(/\n/g, "<br/>");
# 	}
# 	if (!gIE5) {
# 		var urlmatch = new RegExp("((?:http|https|ftp|mms|rtsp)://(&(?=amp;)|[A-Za-z0-9\./=\?%_~@#:;\+\-])+)", "ig");
# 		s = s.replace(urlmatch, "<a target=\"_blank\" href=\"$1\">$1</a>");
# 	}

def genSearchList(infile):
    ret = []
    for line in infile.readlines():
        line=line.strip()
        mat = re.search("ta\.r\('(\d+)','.*','<a href=\"bbsqry\.php\?userid=(\S+)\">\S+</a>.*'<a href=\"(\S+)\">.*</a>'\);",line)
        if mat:
            iid = int(mat.group(1))
            username = mat.group(2)
            url = WEBURL+mat.group(3)
            ret.append((iid,username,url))
    return ret
        
import sys,getopt
def errarg():
    print sys.argv[0]+" [-d day] [-b board] [-v]"
    sys.exit(1)

if __name__ == "__main__":
    # parse command line
    opts, args = getopt.getopt(sys.argv[1:], 'd:b:v')
    for optpair in opts:  # like this[('-d', ''), ('-k', '3')...]
        if optpair[0]=='-v':
            logging.getLogger().setLevel(logging.DEBUG)
        elif optpair[0] =='-d':
            try:
                DAY = (optpair[1])
            except ValueError:
                errarg()
        elif optpair[0] == '-b':
            BOARD = optpair[1]
    
    print 'Board days [default:%s %s]:'%(DAY,BOARD),
    if len(sys.argv) < 2:
        inp = raw_input().strip().split()
        print inp
        if len(inp)>0:
            try:
                DAY = int(inp[0])
                if len(inp)>1:
                    BOARD = inp[1]
            except:
                pass
    else:
        print ""
    lt = genSearchList(urllib.urlopen(WEBURL+"bbsbfind.php?q=1&board=%s&title=&title2=&title3=&userid=&dt=%s&ag=on"%(BOARD,DAY)))
    print '%s jobs to do'%len(lt)
    logger.info("last run within %s days from %s at %s"%(DAY,time.ctime(),BOARD))
    newfile = open(curpath+'newimages.html', 'w')
    newfile.write('<body onload="change()">\n')
    for x in lt:
        try:
            parseURL(x[2], x[1], newfile)
        except KeyboardInterrupt:
            print "sure to exit ? (y/n)",
            ask = raw_input().strip().lower()
            if ask.startswith('y'):
                newfile.write(r'''</body><script language="javascript">
function change(){
	var ims = document.images;
	for(var i=0;i<ims.length; i++){
		if (parseInt(ims[i].naturalWidth) > parseInt(document.width)) {
			ims[i].width = document.width;
		}
	}
}</script>''')
                newfile.close()
                sys.exit(1)
        except:
            logger.error(None)
            pass
    newfile.write(r'''</body><script language="javascript">
function change(){
	var ims = document.images;
	for(var i=0;i<ims.length; i++){
		if (parseInt(ims[i].naturalWidth) > parseInt(document.width)) {
			ims[i].width = document.width;
		}
	}
}</script>''')
    newfile.close()


